var app = getApp();

Page({
  data: {
    userName: '',
    userAge: '',
    partyFace: '入党积极分子',
    partyTime: '2016-09-01',

    genderList: ['女', '男'],
    genderIndex: 0,
    ageList: [],
    ageIndex: 20,
    partyFaceList: ['入党积极分子', '预备党员', '党员'],
    partyFaceIndex: 0,




  },
  onShareAppMessage: function () {
    return {
      title: '重庆大学体育学院研究生支部委员会',
      path: 'cqdx_communist/pages/index/index'
    }
  },
  onLoad: function () {
    //制作年龄数组
    let arr = new Array(100);
    for (let i=0;i<arr.length;i++){
        arr[i] = i;
    }
    this.setData({ageList: arr})

    
    app.util.request({
      url: 'entry/wxapp/UserInfo',
      data: {
        openid: app.globalData.userInfo.openId
      },
      success: (res) => {
        console.log(res.data.data.userinfo);
        let r = res.data.data.userinfo;
        if(r.personname)this.setData({userName: r.personname});
        if(r.gender)this.setData({genderIndex: this.data.genderList.indexOf(r.gender)});
        if(r.age)this.setData({ageIndex: r.age});
        if(r.politystatus)this.setData({partyFaceIndex: this.data.partyFaceList.indexOf(r.politystatus)});
        if(r.jointime)this.setData({partyTime: r.jointime});
      }
    })
  },


  //自定义点击事件
  bindKeyInput(e) {
    this.setData({userName: e.detail.value})
  },
  handleChangePartyTime(e) {
    this.setData({partyTime: e.detail.value})
  },
  handleChangePartyFace(e) {
    this.setData({partyFaceIndex: e.detail.value})
  },
  handleChangeGender(e) {
    this.setData({genderIndex: e.detail.value})
  },
  handleChangeAge(e) {
    this.setData({ageIndex: e.detail.value})
  },
  handleSubmit(e) {
    let name = this.data.userName
    let gender = this.data.genderIndex
    let age = this.data.ageList[this.data.ageIndex]
    let partyFace = this.data.partyFaceList[this.data.partyFaceIndex]
    let time = this.data.partyTime
    let openid = app.globalData.userInfo.openId

    let nameReg = /^[\u4E00-\u9FA5]{2,4}$/
    if(!nameReg.test(this.data.userName)) {
      wx.showToast({
        title: '请输入正确的姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    app.util.request({
      url: 'entry/wxapp/UserInfo',
      data: {
        personname: name,
        gender: gender,
        age: age,
        politystatus: partyFace,
        jointime: time,
        openid: openid 
      },
      success: (res) => {
        
      }
    })
  }
})